using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace TroTro
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TroTro : Vehicle
    {
        #region Fields

        public Rectangle LeftSide;
        public Rectangle RightSide;
        private Game1 _game;

        public TroTro(Game1 game)
            : base(game)
        {
            _game = game;
            LoadContent();
        }

        #endregion

        #region Methods

        protected sealed override void LoadContent()
        {
            Texture = Game.Content.Load<Texture2D>(@"sprites\trotro");
            LeftSide = new Rectangle(0, 0, Texture.Width / 2, Texture.Height);
            RightSide = new Rectangle(Texture.Width / 2, 0, Texture.Width / 2, Texture.Height);
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {

            var kb = Keyboard.GetState();
            if (kb.IsKeyDown(Keys.Up))
            {
                var moveDir = new Vector2((float)Math.Cos(Rotation - MathHelper.PiOver2), 
                                          (float)Math.Sin(Rotation - MathHelper.PiOver2));
                //Position += (moveDir * Speed);


                if(RightBackTireHealth > 50)
                {
                    Position += (moveDir * Speed);
                    _game.CarSpeed += 0.01F;
                }
                else if(LeftBackTireHealth > 50)
                {
                    Position += (moveDir * Speed);
                    _game.CarSpeed += 0.01F;
                }
                else
                {
                    Position -= (moveDir * Speed);
                    _game.CarSpeed += -1F;
                }
            }

            if (kb.IsKeyDown(Keys.Up) && kb.IsKeyDown(Keys.Left))
            {
                Rotation -= TurnSpeed;
            }
            else if(kb.IsKeyDown(Keys.Left))
            {
                //Changes the turning ability depending on tire damage
                if(LeftTireHealth > 250)
                {
                    Position.X -= Speed;
                    Rotation = (float)(MathHelper.TwoPi - 0.40);
                }
                else
                {
                    Position.X -= Speed;
                    Rotation = (float)(MathHelper.TwoPi - 0.10);
                }
            }

            if (kb.IsKeyDown(Keys.Up) && kb.IsKeyDown(Keys.Right))
            {
                Rotation += TurnSpeed;
            }
            else if(kb.IsKeyDown(Keys.Right))
            {
                //Changes the turning ability depending on the tire damage
                if(RightTireHealth > 250)
                {
                    Position.X += Speed;
                    Rotation = 0.40F;
                }
                else
                {
                    Position.X += Speed;
                    Rotation = 0.10F;
                }
            }

            if (kb.IsKeyDown(Keys.Down))
            {
                var moveDir = new Vector2((float)Math.Cos(Rotation + MathHelper.PiOver2), (float)Math.Sin(Rotation + MathHelper.PiOver2));
                Position += (moveDir * (Speed / 3));

                _game.CarSpeed -= 0.02F;
            }

            if (kb.IsKeyUp(Keys.Left) && kb.IsKeyUp(Keys.Right) && kb.IsKeyUp(Keys.Up))
            {
                Rotation = 0;
            }
            base.Update(gameTime);
        }

        #endregion

        #region Accelerate

        public void Accelerate(float magnitude)
        {
            var impulse = Vector2.Zero;

            impulse.X = (float)Math.Sin(Rotation) * magnitude;

            impulse.Y = -(float)Math.Cos(Rotation) * magnitude;

            Velocity += impulse;
        }

        #endregion

        #region Bounds

        public Rectangle GetLeftBounds()
        {
            //Creates the rect for the left front tire
            return new Rectangle((int) (Position.X - (Texture.Width/2) - 4), (int) (Position.Y + (Texture.Height/2)), 4, Texture.Height/2);
        }

        public Rectangle GetRightBounds()
        {
            //Creates the rect for the right front tire
            return new Rectangle((int)Position.X + (Texture.Width/2) + 4, (int)Position.Y + (Texture.Height/2), -4, Texture.Height/2);
        }

        public Rectangle GetLeftRearBounds()
        {
            //Creates the rect for the left rear tire
            return new Rectangle((int)(Position.X - (Texture.Width/2) - 4), (int)(Position.Y - (Texture.Height/2)), 4, Texture.Height/4);
        }

        public Rectangle GetRightRearBounds()
        {
            //Creates the rect for the right rear tire
            return new Rectangle((int)(Position.X + (Texture.Width/2) + 4), (int)(Position.Y - (Texture.Height/2)), -4, Texture.Height/4);
        }

        #endregion
    }
}
